package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.domain.Administrator;
import com.example.domain.User;
import com.example.service.AdministratorService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AccountController {

	private final UserService userService;

	private final AdministratorService administratorService;

	@Autowired
	public AccountController(UserService userService, AdministratorService administratorService) {
		this.userService = userService;
		this.administratorService = administratorService;
	}

	/**
	 * 接收前端输入的注册信息，并将其添加至数据库，其中telephone不能重复
	 *
	 * @param user 经过跨域传输的用户的注册信息
	 * @return 根据不同的情况，返回不同的JSON字符串
	 */
	@PostMapping(value = "/userRegister", produces = "application/json;charset=utf-8")
	public String register(@RequestBody User user) {
		Map<String, Object> message = new HashMap<>(16);
		User existedUser = userService.getUserByTelephone(user.getTelephone());

		if (existedUser == null) {
			userService.save(user);
			message.put("message", 1);
		} else {
			message.put("message", "对不起，此电话号码已经被注册，请重新输入电话号码进行注册");
		}

		return JSON.toJSONString(message);
	}

	/**
	 * 验证前端输入的电话、密码信息与数据库中信息是否一致
	 *
	 * @return 根据不同的情况，返回不同的JSON字符串
	 */
	@PostMapping(value = "/userLogin", produces = "application/json;charset=utf-8")
	public String login(@RequestBody User acceptUser) {
		Map<String, Object> message = new HashMap<>(16);
		User user = userService.getUserByTelephone(acceptUser.getTelephone());
		if (user != null && user.getPassword().equals(acceptUser.getPassword())) {
			if (user.getAuditRegistryState() == 1) {
				if (user.getEnableState() == 0) {
					message.put("message", "验证成功");
					message.put("identity", user.getIdentity());
					message.put("user", user);
				} else if (user.getEnableState() == 1) {
					message.put("message", "对不起，此账号的状态的为已禁用");
				}
			} else if (user.getAuditRegistryState() == 0) {
				message.put("message", "对不起，此账号虽然已经注册但管理员尚未审核");
			} else if (user.getAuditRegistryState() == 2) {
				message.put("message", "对不起，此账号虽然已经注册但没有通过管理员的审核");
			}
		} else {
			message.put("message", "您输入的电话号码或密码有误，请重新输入");
		}
		return JSON.toJSONString(message);
	}

	/**
	 * 根据id查询用户
	 *
	 * @param id 用户id
	 * @return 返回JSON数据
	 */
	@PostMapping(value = "/user/general", produces = "application/json;charset=utf-8")
	public String getUserById(String id) {
		HashMap<String, Object> message = new HashMap<>(16);
		User user = userService.getById(Integer.parseInt(id));
		message.put("user", user);

		return JSON.toJSONString(message);
	}

	/**
	 * 验证前端输入的管理员账号、管理员密码信息与数据库中信息是否一致
	 *
	 * @return 根据不同的情况返回不同的JSON字符串
	 */
	@PostMapping(value = "/administratorLogin", produces = "application/json;charset=utf-8")
	public String adminLogin(@RequestBody Administrator acceptAdministrator) {
		Map<String, Object> message = new HashMap<>(16);

		Administrator administrator = administratorService.getByAdminId(acceptAdministrator.getAdministratorId());

		if (administrator != null && administrator.getPassword().equals(acceptAdministrator.getPassword())) {
			message.put("message", "验证成功");
		} else {
			message.put("message", "您输入的管理员账号或密码有误，请重新输入");
		}
		return JSON.toJSONString(message);
	}

	/**
	 * 更新用户个人信息
	 *
	 * @param userId  用户id
	 * @param email   用户邮箱
	 * @param address 用户地址
	 * @return 返回更新结果
	 */
	@PostMapping(value = "/personal/information/update", produces = "application/json;charset=utf-8")
	public String updateUserInfo(Integer userId, String email, String address) {
		HashMap<String, Object> message = new HashMap<>(16);
		User user = userService.getById(userId);
		user.setEmail(email);
		user.setAddress(address);
		userService.updateById(user);
		message.put("message", 1);

		return JSON.toJSONString(message);
	}

	/**
	 * 修改用户密码
	 *
	 * @param userId   用户id
	 * @param password 新密码
	 * @return 返回修改结果
	 */
	@PostMapping(value = "/personal/password/update", produces = "application/json;charset=utf-8")
	public String updatePassword(Integer userId, String password) {
		HashMap<String, Object> message = new HashMap<>(16);
		User user = userService.getById(userId);
		user.setPassword(password);
		userService.updateById(user);
		message.put("message", 1);

		return JSON.toJSONString(message);
	}


}
